[

// add map->seek
define map->seek(key::string,default) => {
	.keys !>> #key ? .insert(#key = #default)
	return(.find(#key))
}

/*

var('map') = map;
$map->seek('Volvo',map)->seek('Silver',string);
$map;

*/

// ============================================

// get map element based on position
// code from Fletcher Sandbeck
define map->getpos(pos::integer) => {
	local(n = .getnode(.keys->get(#pos)))
	return pair(#n->key = #n->value)
}

// ============================================

define map->each(code::capture) => {
  .forEachPair => {
    this = #1
    #code->invoke
    .insert(this->name = this->value)
  }
}

/*
var('map') = map('a'=1,'b'='d','c'=date)
$map->each({^ this->asString + ' ' ^})
'<br>';
var('map') = map('a'=1,'b'=2,'c'=3)
$map->each({ this->second *= 5 })
$map
*/


// ============================================


// ===== PATCHES =====

// fix map->asArray to return the pairs, not the values
define map->asArray::array => {
  local(array) = array
  .forEachPair => {
    #array->insert(#1)
  }
  return #array
}
//map(1='a',2='b',3='c')->asArray

define map->merge(map::map)=> {
  #map->forEachPair => {
    .insert(#1)
  }
}

define map->merge(array::array)=> {
  #array->forEach => {
    .insert(#1)
  }
}


]